package com.bianguo.android.eventbuslibrary;

import android.arch.lifecycle.MutableLiveData;

import java.util.HashMap;

public class LiveDataBus {

    private static volatile LiveDataBus instance;
    private HashMap<String, MutableLiveData<Object>> busMap;

    private LiveDataBus() {
        busMap = new HashMap<>();
    }

    public static LiveDataBus getInstance() {
        LiveDataBus bus = instance;
        if (bus == null) {
            synchronized (LiveDataBus.class) {
                if (bus == null) {
                    bus = new LiveDataBus();
                    instance = bus;
                }
            }
        }
        return bus;
    }


    public <T> MutableLiveData<T> getChannel(String target, Class<T> type) {
        if (!busMap.containsKey(target)) {
            busMap.put(target, new MutableLiveData<>());
        }
        return (MutableLiveData<T>) busMap.get(target);
    }

    public MutableLiveData<Object> getChannel(String target) {
        return getChannel(target, Object.class);
    }
}
